// CommandLineHelper Library
// =========================
// License: Microsoft Permissive License (Ms-PL) v1.1
// Author: Rhy Mednick (http://www.rhyverse.net)
// Project Site: http://www.codeplex.com/CommandLineHelper

using System;
using System.Collections.Generic;
using System.Text;

namespace CommandLineHelper.Attributes
{
    /// <summary>
    /// When applied to a property, this attribute instructs the command line helper parsing routine to validate 
    /// that the property value passed in is not null or an empty string.  This attribute can only be applied 
    /// to string properties.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class ValidateNotNullOrEmptyAttribute : Attribute, IValidation
    {
        private string m_statusMessage = String.Empty;

        /// <summary>
        /// Gets the status message that is set when the IsValid() method is called.
        /// </summary>
        public string StatusMessage
        {
            get
            {
                return m_statusMessage;
            }
        }

        /// <summary>
        /// Validates that the value is not null or empty.
        /// </summary>
        /// <param name="value">The value to validate.</param>
        /// <returns>True if the value is not null or empty and false if it is.</returns>
        public bool IsValid(object value)
        {
            bool retval = false;

            //if we're not null, then we're good unless we're an empty string
            if (value != null)
            {
                string strval = value as string;
                if (strval != null)
                {
                    //we're a string, so make sure we're not empty
                    if (strval != string.Empty)
                    {
                        retval = true;
                    }
                }
                else
                {
                    //we're not null and not a string, so we're valid
                    retval = true;
                }
            }

            if (!retval)
            {
                m_statusMessage = "The property is empty or null.";
            }

            return retval;
        }

    }
}
